﻿using QuanLyThuVien.BLL;
using QuanLyThuVien.Supporter;
using QuanLyThuVien.DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThuVien.GUI
{
    public partial class GUIDangNhap : DevComponents.DotNetBar.Office2007Form
    {
        private BLLNguoiDung _bllNguoiDung;

        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public GUIDangNhap()
        {
            InitializeComponent();

            _bllNguoiDung = new BLLNguoiDung();
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện click DangNhap
        //----------------------------------------------------------------------
        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (_txtTaiKhoan.Text == "")
            {
                MessageBox.Show("Chưa nhập tài khoản");
                _txtTaiKhoan.Focus();
                return;
            }

            if (_txtMatKhau.Text == "")
            {
                MessageBox.Show("Chưa nhập mật khẩu");
                _txtMatKhau.Focus();
                return;
            }

            DTONguoiDung lastUser = null;
            if (UserManager.User != null && UserManager.User.TaiKhoan != "" && UserManager.User.MatKhau != "")
                lastUser = UserManager.User;

            DTONguoiDung newUser = _bllNguoiDung.LayThongTinNguoiDung(_txtTaiKhoan.Text, Utils.MaHoa(_txtMatKhau.Text));
            if (newUser != null)
            {
                MessageBox.Show("Đăng nhập thành công");
                UserManager.User = newUser;
                this.Close();
            }
            else
            {
                MessageBox.Show("Đăng nhập thất bại");
                if (lastUser != null)
                    UserManager.User = lastUser;
            }
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện click Thoat
        //----------------------------------------------------------------------
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện check HienMatKhau
        //----------------------------------------------------------------------
        private void chbHienMatKhau_CheckedChanged(object sender, EventArgs e)
        {
            if (_chbHienMatKhau.Checked)
                _txtMatKhau.UseSystemPasswordChar = false;
            else
                _txtMatKhau.UseSystemPasswordChar = true;
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện key press
        //----------------------------------------------------------------------
        private void Special_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Utils.IsSpecialKey(e.KeyChar))
                e.Handled = true;
            else
                e.Handled = false;
        }
    }
}
